package org.cogate.thrift.server.conf;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import javax.annotation.PostConstruct;

import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.cogate.thrift.server.serviceImpl.HelloServiceImpl;
import org.cogate.thrift.server.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cupdata.thrift.zicon.interfaces.HelloService;
import com.cupdata.thrift.zicon.interfaces.UserService;
import com.cupdata.thrift.zicon.interfaces.UserService.Processor;

public class ThriftConfig {
	
/*	@Value("${thread.pool.size}")
    int threadPoolSize;
	
	@Value("${time.to.sleep}")
	long timeToSleep;
	
	@Value("${server.socket.port}")
	int tServerSocketPort;
	
	ThreadPoolExecutor  executor;

	@Bean
	public TServerTransport tServerTransport() {
		try {
			return new TServerSocket(tServerSocketPort);
		} catch (TTransportException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Bean
	public TServer tServer() {
		//发布多个接口的服务实现类
		@SuppressWarnings("unchecked")
		TMultiplexedProcessor processor = new TMultiplexedProcessor();
		processor.registerProcessor("UserService", new UserService.Processor<UserService.Iface>(new UserServiceImpl()));
		processor.registerProcessor("HelloService", new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl()));
//		UserService.Processor processor = new UserService.Processor(
//				new UserServiceImpl());
//		HelloService.Processor hsProcessor = new 
		TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(
				tServerTransport()).processor(processor));
		return server;
	}
	
	
	@PostConstruct
	public void init(){
		executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadPoolSize);
		executor.execute(new Runnable() {
			@Override
			public void run() {
				tServer().serve();	
			}
		});
	}*/

}
